%%%%%%%%%%%
%% Alternatives ...
%%%%%%%%%%%kalm
close all;
clear all;

% addpath D:\apps\Iris\Archive\IRIS_Tbx_20140320; irisstartup;
% addpath D:\apps\Iris\Archive\IRIS_Tbx_20130523; irisstartup;

%% Reads the model
[m,p,mss] = readmodel(false);

%% Data
% Loads the baseline scenario
d = dbload('fcastdata.csv');

% Loads the the historical data from the file '.csv'
% (the file 'kalm_his.csv' is saved by the 'kalmanfilter' program)
h = dbload('kalm_his.csv');

% Defines the time frame of the forecast
% Don't forget to change the time frame depending on you data and forecast
% period!
startfcast = qq(2013,4);
endfcast = qq(2020,4);
fcastrange = startfcast:endfcast;

%% Codes for alternative scenarios for which "shocks" are used 

% Simulate alternative scenario when shocks are expected, i.e. are implied
% by other governmental policies (announced)
% s = simulate(m,h,fcastrange,'anticipate',true,'nonlinearise=',30,'maxiter=',1000);

%% Codes for alternative scenarios for which "plan" is used
% Define the "plan"
% simplan = plan(m,startfcast:endfcast);

%% Codes for the "baseline" forecast (copied from forecast.m)
simplan = plan(m,startfcast:endfcast);


% External variables ("the rest of the world")

% h.dot_x_cpi(startfcast:startfcast+8) = [1,1,1,1,1,1,1,1,1]
% h.x_rn(startfcast:startfcast+3) = [0.25,0.25,0.25,0.25];
% h.lx_gdp_gap(startfcast:startfcast+8) = [-2.7,-2.6,-2.5,-2,-1.5,-1.5,-1,-1,0];
% simplan = exogenize(simplan,{'dot_x_cpi'},startfcast:startfcast+8);
% simplan = exogenize(simplan,{'x_rn'},startfcast:startfcast+3);
% simplan = exogenize(simplan,{'lx_gdp_gap'},startfcast:startfcast+8);
% simplan = endogenize(simplan,{'e_dot_x_cpi'},startfcast:startfcast+8);
% simplan = endogenize(simplan,{'e_x_rn'},startfcast:startfcast+3);
% simplan = endogenize(simplan,{'e_lx_gdp_gap'},startfcast:startfcast+8)

% Domestic policy variables (temporary ER peg, interest rate "forward
% guidance")

% h.ls(startfcast:startfcast+4) = [329,329,329,329,329];
% h.rn(startfcast:startfcast+4) = [0.46,0.46,0.46,0.46,0.46];
% simplan = exogenize(simplan,{'ls'},startfcast:startfcast+4);
% simplan = exogenize(simplan,{'rn'},startfcast:startfcast+4);
% simplan = endogenize(simplan,{'e_ls'},startfcast:startfcast+4)
% simplan = endogenize(simplan,{'e_rn'},startfcast:startfcast+4)

% NTF

% h.dot_cpi_x(startfcast:startfcast+1) = [0,0];
% simplan = exogenize(simplan,{'dot_cpi_x'},startfcast:startfcast+1);
% simplan = endogenize(simplan,{'e_dot_cpi_x'},startfcast:startfcast+1);
% 
% h.dot_cpi_food(startfcast:startfcast+1) = [0,0];
% simplan = exogenize(simplan,{'dot_cpi_food'},startfcast:startfcast+1);
% simplan = endogenize(simplan,{'e_dot_cpi_food'},startfcast:startfcast+1);
% 
% h.dot_cpi_oil(startfcast:startfcast+1) = [0,0];
% simplan = exogenize(simplan,{'dot_cpi_oil'},startfcast:startfcast+1);
% simplan = endogenize(simplan,{'e_dot_cpi_oil'},startfcast:startfcast+1);


%% Alternative scenario (additional assumptions relative to forecast.m)

% Fiscal impulse for 4 quarters
% h.e_lgdp_gap = tseries(startfcast+1:startfcast+3,0.5);

% Capital inflows for 2 quarters
% h.e_prem = tseries(startfcast+1:startfcast+2,2);
% h.e_cr_prem = tseries(startfcast+1:startfcast+4,0.5);
% Shock to risk premium
%  h.e_prem = tseries(startfcast:startfcast+2,4);

% Shock to inflation (e.g., VAT increase, administrative prices, etc.)
% h.e_dot_cpi_x = tseries(startfcast:startfcast+1,[0,1]);
% h.e_dot_cpi_food = tseries(startfcast:startfcast+1,[0,1]);
% h.e_dot_cpi_oil = tseries(startfcast:startfcast+1,[0,1]);

% Shock to aggregate demand (e.g., sharp cyclical slowdown, fiscal tightening)
% h.e_lgdp_gap = tseries(startfcast+1:startfcast+3,-5);

%% Simulate alternative scenario when shocks are not expected
%  s = simulate(m,h,fcastrange,'anticipate',false,'nonlinearise=',30,'maxiter=',1000);

s = simulate(m,h,fcastrange,'plan',simplan,'nonlinearise=',30,'maxiter=',1000);


%% Additional options for foreign variables are defined here

% Defining world food prices
% h.dot_wfood(startfcast:startfcast+3) = [-10,-5,0,0];
 
% Fixing world food prices
% simplan = exogenize(simplan,{'dot_wfood'},startfcast:startfcast+3);
% simplan = endogenize(simplan,{'e_dot_wfood'},startfcast:startfcast+3);

% Defining world oil prices
% h.dot_woil(startfcast:startfcast+3) = [-20,-10,0,0];
 
% Fixing world oil prices
% simplan = exogenize(simplan,{'dot_woil'},startfcast:startfcast+3);
% simplan = endogenize(simplan,{'e_dot_woil'},startfcast:startfcast+3);

%% Data for foreign variables defined in 'data.csv'
% Exact quarter till when the data is available in 'data.csv' must be specified
% xrange = startfcast:qq(2012,4)

% simplan = exogenize(simplan,{'dot_x_cpi','x_rn',...},xrange);
% simplan = endogenize(simplan,{'e_dot_x_cpi','e_x_rn',...},xrange);


%% Simulate alternative scenario
% Evolution of foreign variables is an assumption, i.e. it is a central bank
% expectation of future
% s = simulate(m,h,fcastrange,'anticipate',true,'plan',simplan,'nonlinearise=',30,'maxiter=',1000);

%%
% Command 'dboverlay' puts together the historical database 'h' and the
% results of the simulation saved in object 's'. Single database 's' is
% created
s.rn = s.rs;
h = dbextend(h,s);

%% Additional forecast-implied data to be saved
% Oil prices, level, USD/barrel
% USD/EUR exchange rate, level
for i = startfcast:endfcast
h.lwoil(i) = h.lwoil(i-1) + h.dot_woil(i)/4;
h.woil(i) = exp(h.lwoil(i)/100);
h.ls_cross(i) = h.ls_cross(i-1) + h.dot_s_cross(i)/4;
h.s_cross(i) = exp(h.ls_cross(i)/100);
end

%% Merging baseline and alternative databases
f = and(d, h);

%% Results are saved in file 'fcastdata_alt.csv'  
dbsave('fcastdata_alt.csv',f);

%% Graphs and Tables
% Prepares the report: graphs and tables in the Acrobat .pdf format
Tablerng = startfcast-3:startfcast+7;
Plotrng = startfcast-3:startfcast+11;
Histrng = startfcast-3:startfcast-1;

% Specify country and units for exchange rate
country = 'The Czech Republic';
exchange = 'CZK/EUR';

% Specify alternative, i.e. oil, food, etc.
% alternative = 'Baseline vs. Fiscal stimulus';
alternative = 'Baseline vs. Capital outflows';

% Report
x = report.new(country,'marks',{'Baseline','Alternative'});

% Figures
sty = struct();
sty.line.linewidth = 1.5;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'k'};
sty.axes.box = 'on';
sty.legend.box = 'off';
sty.legend.edgecolor = 'white';

dTicks = Plotrng(1):4:Plotrng(end);

x.figure(alternative,'subplot',[3,3],'style',sty,'range',Plotrng,...
  'dateformat','YYYY:P','dateTick',dTicks);

x.graph('Inflation (q-o-q)','legend',true,'legendLocation','bottom');
x.series('',f.dot_cpi);
x.highlight('',Histrng);

x.graph('Inflation (y-o-y)','legend',false);
x.series('',f.dot4_cpi);
x.highlight('',Histrng);

x.graph('Core Inflation (q-o-q)','legend',false);
x.series('',f.dot_cpi_x);
x.highlight('',Histrng);

x.graph('Food Prices (q-o-q)','legend',false);
x.series('',f.dot_cpi_food);
x.highlight('',Histrng);

x.graph('Oil Prices (q-o-q)','legend',false);
x.series('',f.dot_cpi_oil);
x.highlight('',Histrng);

x.graph('Nominal Interest Rate','legend',false);
x.series('',f.rn);
x.highlight('',Histrng);

x.graph(['Nominal Exchange Rate - ' exchange],'legend',false);
x.series('',exp(f.ls/100));
x.highlight('',Histrng);

x.graph('Output Gap','legend',false);
x.series('',f.lgdp_gap);
x.highlight('',Histrng);

x.graph('Monetary Conditions','legend',false);
x.series('',f.mci);
x.highlight('',Histrng);

x.pagebreak();

% Tables
TableOptions = {'range',Tablerng,'vline',startfcast-1,'decimal',1,'dateformat','YYYY:P',...
    'long',true,'longfoot','---continued','longfootposition','right'};

x.table([alternative ' - Main Indicators'],TableOptions{:});

x.subheading('CPI Inflation');
  x.series('CPI ',f.dot4_cpi,'units','% (y-o-y)');
  x.series('',f.dot_cpi,'units','% (q-o-q)');
x.subheading('');  
  x.series('Core Inflation',f.dot4_cpi_x,'units','% (y-o-y)');
  x.series('',f.dot_cpi_x,'units','% (q-o-q)');
x.subheading('');
  x.series('Food Prices',f.dot4_cpi_food,'units','% (y-o-y)');
  x.series('',f.dot_cpi_food,'units','% (q-o-q)');
  
x.subheading('');
  x.series('Oil Prices',f.dot4_cpi_oil,'units','% (y-o-y)');
  x.series('',f.dot_cpi_oil,'units','% (q-o-q)');
  
x.subheading('Nominal Interest Rates');
  x.series('Policy Rate',f.rn,'units','% p.a.');
  x.series('Policy Neutral Rate',f.rn_neutral,'units','% p.a');
  
x.subheading('Nominal Exchange Rate');
  x.series(exchange,exp(f.ls/100),'units','level');
  x.series('',f.ls - f.ls{-4},'units','% (y-o-y)');
  x.series('',f.dot_s,'units','% (q-o-q)');

x.pagebreak();

x.table([alternative ' - Main Indicators'],TableOptions{:});

x.subheading('Real Economy');
  x.series('Output Gap',f.lgdp_gap,'units','%');
  x.series('GDP Growth',f.lgdp - f.lgdp{-4},'units','% (y-o-y)');
  x.series('Core Inflation',f.dot_cpi_x,'units','% (q-o-q)');

x.subheading('Monetary Conditions');
  x.series('Monetary Conditions',f.mci,'units','%');
  x.series('Real Interest Rate Gap ',f.rr_gap,'units','p.p.');
  x.series('Real Exchange Rate Gap',f.lz_gap,'units','%');
  
x.pagebreak();

x.table([alternative 'Foreign Variables'],TableOptions{:});

x.subheading('European Monetary Union');
  x.series('Inflation',f.dot_x_cpi,'units','% (q-o-q)');
  x.series('Interest Rate',f.x_rn,'units','%');
  x.series('Output Gap',f.lx_gdp_gap,'units','%');

x.subheading('World Food and Oil Prices');
  x.series('IMF Food Price Index',f.dot_wfood,'units','% (q-o-q)');
  x.series('Oil Prices',f.dot_woil,'units','% (q-o-q)');
  x.series('',f.woil,'units','USD/Barrel');
  
x.publish('Compare','display',false);
disp('Done!');